/**
 * 业务字典管理初始化
 */
var CdcDictionary = {
    id: "CdcDictionaryTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    dictypeId:"",
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
CdcDictionary.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '代码', field: 'code', align: 'center', valign: 'middle', sortable: true},
        {title: '描述', field: 'description', align: 'center', valign: 'middle', sortable: true},
        // {title: '字典类型', field: 'dictype.description', align: 'center', valign: 'middle', sortable: true},
        {title: '是否有效', field: 'isActive', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index) {
            return value?"是":"否";
        }},
        {title: '操作', field: 'id', align: 'center', width: '120px', valign: 'middle',formatter:function(value,row,index){
    		var _html = '<a href="javascript:CdcDictionary.edit(\''+row.id+'\')" title="修改"><i class="hisui-icon icon-write-order"></i></a>';
    		_html += '<a href="javascript:CdcDictionary.del(\''+row.id+'\',\''+row.name+'\')" title="删除"><i class="hisui-icon icon-cancel"></i></a>';
    		return _html;
        }}];
};

/**
 * 检查是否选中
 */
CdcDictionary.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        CdcDictionary.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增业务字典
 */
CdcDictionary.add = function () {
    if (!CdcDictionary.dictypeId) {
        Sys.error("新增失败!请先选择字典类型类型！");
    } else {
        var index = Sys.open('新增业务字典',Sys.backendPath + '/cdc/cdcdictionary/to_add',['800px', '280px']);
        this.layerIndex = index;
    }
};

/**
 * 打开查看业务字典详情
 */
CdcDictionary.edit = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
    	var index = Sys.open('业务字典详情',Sys.backendPath + '/cdc/cdcdictionary/to_update/' + id,['800px', '280px']);
        this.layerIndex = index;
    }
};

/**
 * 删除业务字典
 */
CdcDictionary.del = function (id,name) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
	if (flag) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/cdc/cdcdictionary/delete", function (data) {
	            Sys.success("删除成功!");
	            CdcDictionary.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("cdcDictionaryId",id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除业务字典?",operation);
    }
};

/**
 * 查询业务字典列表
 */
CdcDictionary.search = function () {
    var queryData = {};
    queryData['description'] = $("#description").val();
    CdcDictionary.table.refresh({query: queryData});
};
CdcDictionary.treeClick=function(e, treeId, treeNode) {
   if(!treeNode.isParent){
       CdcDictype.dataId=treeNode.id;
       CdcDictype.table.clear();
       CdcDictype.table.refresh({query:{dataId:treeNode.id}});
       CdcDictionary.table.clear();
       CdcDictionary.table.refresh({query: {dictypeId:0}});
   }
}
CdcDictionary.enter=function(id,value) {
    if (!value) {
        value="null";
    }
    CdcDictionary.ztree.setUrl(Sys.backendPath + "/cdc/cdcdata/tree/"+value+"/null");
    CdcDictionary.ztree.init();
    $("#"+id).searchbox('setValue', '');
}
$(function () {
    var defaultColunms = CdcDictionary.initColumn();
    var table = new BSTable(CdcDictionary.id, Sys.backendPath + "/cdc/cdcdictionary/list", defaultColunms);
    table.setPaginationType("server");
    table.setPageSize(10);
    table.setQueryParams({ NotQuery: true });
    CdcDictionary.table = table.init();
    var ztree = new $ZTree("prodataTree", Sys.backendPath + "/cdc/cdcdata/tree/null/null");
    ztree.bindOnClick(CdcDictionary.treeClick);
    ztree.init();
    CdcDictionary.ztree = ztree;
});
